﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Xml.Serialization;
using System.IO;

namespace Urbania.GUI.Elements
{
    [Serializable]
    [XmlRoot("Button")]
    public class Button : GUIElement
    {
        public enum DesignType { Type1 = 1, Type2 };

        protected Rectangle bounds;
        protected String text;
        protected DesignType designType;
        protected Texture2D sprite;
        protected SpriteFont font;
        protected Color fontColor;
        protected String scriptFilePath;
        protected LuaVM lua;
        protected string buttonName;

        public string Title { get { return this.buttonName; } set { this.buttonName = value; } }
        public string Text { get { return this.text; } set { this.text = value; } }
        public int DesignTypeInt { get { return (int)this.designType; } set { this.designType = (DesignType)value; } }
        public string Script { get { return this.scriptFilePath; } set { this.scriptFilePath = value; } }
        public Rectangle Bounds { get { return this.bounds; } set { this.bounds = value; } }

        public Button()
        {
            this.sprite = Utilities.Loader.GetTex("button1");
            this.font = Utilities.Loader.GetFont("sf");
            this.text = "Test";
            this.bounds = new Rectangle(0, 0, (int)(font.MeasureString(this.text).X + 10), (int)(font.MeasureString(this.text).Y + 10));
            this.designType = DesignType.Type1;
            this.fontColor = Color.White;
            this.buttonName = "defualt";
            this.scriptFilePath = null;
        }

        public Button(string buttonName, Vector2 position, String text, int designType, ref LuaVM lua, string scriptPath)
        {
            this.sprite = Utilities.Loader.GetTex("button" + (int)designType);
            this.font = Utilities.Loader.GetFont("sf");
            this.bounds = new Rectangle((int)position.X, (int)position.Y, (int)(font.MeasureString(text).X + 10), (int)(font.MeasureString(text).Y + 10));
            this.text = text;
            this.designType = (DesignType)designType;
            this.fontColor = Color.White;
            this.lua = lua;
            this.scriptFilePath = scriptPath;
            this.buttonName = buttonName;
            Utilities.Loader.LoadScriptFile(this.scriptFilePath);
        }

        public void Update(GameTime gameTime, MouseState mouse)
        {
            if (mouse.X <= bounds.Right && mouse.X >= bounds.X)
                if (mouse.Y <= bounds.Bottom && mouse.Y >= bounds.Y)
                    this.fontColor = Color.Yellow;
                else
                    this.fontColor = Color.White;
            else
                this.fontColor = Color.White;

            if (mouse.LeftButton == ButtonState.Pressed)
                this.OnClick();
        }

        public void Update(GameTime gameTime, Vector2 mouse, bool mouseClicked)
        {
            if (mouse.X <= bounds.Right && mouse.X >= bounds.X)
                if (mouse.Y <= bounds.Bottom && mouse.Y >= bounds.Y)
                    this.fontColor = Color.Yellow;
                else
                    this.fontColor = Color.White;
            else
                this.fontColor = Color.White;

            if (mouseClicked)
            {
                this.OnClick();
            }
        }

        public new void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(sprite, bounds, Color.White);
            spriteBatch.DrawString(font, text, new Vector2(2 + bounds.X, 5 + bounds.Y),fontColor);
        }

        public void OnClick()
        {
            if (scriptFilePath == null)
                return;

            string tempString = Utilities.Loader.GetScriptFile(scriptFilePath);
            lua.LuaVirtual.DoFile(tempString);
        }
    }
}
